# coding:utf-8
"""
products =  [[“iphone”,6888],[“MacPro”,14800],[“小米6”,2499],[“Coffee”,31],[“Book”,60],[“Nike”,699]]
a、打印出以下格式
b、购物车功能：根据的products列表写一个循环不断询问用户想买什么，用户选择一个商品编号，就把对应的商品添加到购物车里，最终用户输入q退出时，打印购买的商品列表。
提示:有shop_car和shop_cost 两个列表，当时输入商品不存在的时候，提示相关错误
![img](http://p.qpic.cn/homework/0/hw_h_4ty9yul3h9mo4ks5fbc663c1a65b/)
"""

products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
# 1,打印格式
print("----- 商品列表 -----")
for item in products:
    print(item[0], item[1], sep="\t")
# 2, 购物车功能
print("编号", "名称", "价格", sep="\t")
i = 1
for item in products:
    print(i, item[0], item[1], sep="\t")
    i += 1

# 定义购物车
shop_car = []
shop_cost = []
sum = 0
while True:
    your_choice = input("想买什么，请输入商品编号(q退出):")
    if your_choice != 'q':
        try:
            your_choice = int(your_choice)
            if your_choice in range(1, 7):
                shop_car.append(products[your_choice-1])
            else:
                print("没有该编号商品")
            print("已购买商品:", shop_car)
        except Exception as e:
            print("商品编号有误!",e)
    else:
        print("已购买商品:", shop_car)
        for item in shop_car:
            sum += item[1]
        shop_cost.append(sum)
        print("总费用:",shop_cost)
        break
